package com.deep.stream.service;

/**
 * @author dell
 * @create 2022/7/28 8:27
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Slf4j
@EnableBinding(value = {OutputInterface.class})
@EnableScheduling
public class MessageSender {

    @Autowired
    private OutputInterface outputInterface;

    /**
     * 第一种方法, 没有指定output的MessageChannel, 通过OutputInterface去拿具体的Channel
     * 设置partitionKey主要是为了分区用, 可以根据根据这个partitionKey来分区
     * 采用定时任务去发送
     */
    @Scheduled(initialDelay = 1000, fixedRate = 5000)
    public void sendMessageMethod1() {
        Message message = MessageBuilder.withPayload("From sendMessageMethod1")
                .setHeader("partitionKey", 1)
                .build();

        outputInterface.output1().send(message);
    }

    /**
     * 第二种方法, 直接指定output的MessageChannel
     */
//    @Autowired
//    @Qualifier(OutputInterface.OUTPUT2)
//    MessageChannel output;
//
//    @Scheduled(initialDelay = 2000, fixedRate = 4000)
//    public void sendMessageMethod2() {
//        Person p = new Person();
//        p.setName( "Person2" );
//        p.setAge( 1 );
//
//        output.send( MessageBuilder.withPayload( p )
//                .setHeader( "partitionKey", 2 )
//                .build() );
//    }
}